package com.zt.questionnaire.controller;

import com.zt.questionnaire.common.HService;
import com.zt.questionnaire.common.annotation.AccountLoginVerify;
import com.zt.questionnaire.common.msg.HResultMsg;
import com.zt.questionnaire.db.entity.UserEntity;
import com.zt.questionnaire.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;


/**
 * 用户
 *
 * @author funnywus
 * @email funnywus@163.com
 * @date 2023-09-01 11:13:06
 */
@Validated
@Slf4j
@RestController
@RequestMapping(value = "/user", produces = "application/json; charset=UTF-8")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    /**
     * https://gitee.com/andocode/weixin-java-miniapp-demo/blob/master/src/main/java/com/github/binarywang/demo/wx/miniapp/controller/WxMaUserController.java
     * 小程序登录接口
     * 1.根据code获取微信openId、unionId信息
     * 2.根据openId，unionId生成用户信息，token，返回给客户端
     *
     * @param code 小程序
     * @return
     */
    @PostMapping(value = "/login", produces = "application/json; charset=utf-8")
    public String login(@NotBlank(message = "code is blank") String code) {
        log.info("code:{}", code);
        UserEntity user = userService.login(code);
        return HResultMsg.createMsgSucc(user);
    }

    /**
     * 获取用户信息接口
     * 1.获取微信用户信息，并且更新微信用户信息到数据库
     *
     * @param sessionKey
     * @param signature
     * @param rawData
     * @param encryptedData
     * @param iv
     * @return
     */
    @AccountLoginVerify
    @PostMapping(value = "/get", produces = "application/json; charset=utf-8")
    public String get(@NotBlank(message = "sessionKey is blank") String sessionKey,
                      @NotBlank(message = "signature is blank") String signature,
                      @NotBlank(message = "rawData is blank") String rawData,
                      @NotBlank(message = "encryptedData is blank") String encryptedData,
                      @NotBlank(message = "iv is blank") String iv
    ) {
        Long userId = HService.getAccountId(request);
        UserEntity user = userService.getWxUserInfo(sessionKey, signature, rawData, encryptedData, iv, userId);
        return HResultMsg.createMsgSucc(user);
    }

    /**
     * 获取微信的用户手机号，并且返回用户对象
     * 1.获取微信用户信息，并且更新微信用户信息到数据库
     *
     * @param code
     * @return
     */
    @AccountLoginVerify
    @PostMapping(value = "/phone/get", produces = "application/json; charset=utf-8")
    public String getPhone(@NotBlank(message = "code is blank") String code) {
        Long userId = HService.getAccountId(request);
        UserEntity user = userService.getPhoneInfo(userId, code);
        return HResultMsg.createMsgSucc(user);
    }

}
